"use strict";

const Controller = require("egg").Controller;
// const fs = require('fs');
const Excel = require("exceljs");
class UserController extends Controller {
  async find() {
    const { ctx } = this;
    const findRule = {
      pageSize: { type: "number", required: true },
      pageNo: { type: "number", required: true },
    };
    ctx.validate(findRule, ctx.request.body);
    const data = await this.service.user.find(ctx.request.body);
    ctx.helper.success(ctx, data);
  }

  async create() {
    const { ctx } = this;
    await this.service.user.create(ctx.request.body);
    ctx.helper.success(ctx);
  }

  async deleteOne() {
    const { ctx } = this;
    await this.service.user.deleteOne(ctx.request.body);
    ctx.helper.success(ctx);
  }

  async update() {
    const { ctx } = this;
    await this.service.user.update(ctx.request.body);
    ctx.body = {
      status: true,
    };
  }
  async login() {
    const { ctx, app } = this;
    const createRule = {
      name: { type: "string", required: true },
      password: { type: "string", required: true },
    };
    ctx.validate(createRule, ctx.request.body);
    const data = await this.service.user.login(ctx.request.body);
    if (data.length === 0) {
      ctx.body = { status: false };
    } else {
      const token = app.jwt.sign(
        {
          name: data.name,
        },
        app.config.jwt.secret,
        {
          expiresIn: "7d",
        }
      );
      ctx.body = { status: true, data: { token } };
    }
  }

  async upload() {
    const { ctx } = this;
    const stream = await ctx.getFileStream();
    const filename = "cloudNine/" + stream.filename;
    const result = await ctx.oss.putStream(filename, stream);
    if (result.res.status === 200) {
      ctx.helper.success(ctx, result.url);
    }
  }

  async download() {
    const { ctx } = this;
    const fileName = "eleme.jpeg";
    const result = await ctx.oss.get(fileName);
    /**
     * Content-Disposition:下载文件的一个标识字段，两种类型：inline 和 attachment
     * Access-Control-Expose-Headers 允许前端跨域后访问的header字段
     */
    ctx.attachment(fileName);
    ctx.set({
      ...result.res.headers,
      // 'Content-Disposition': `attachment; filename=${fileName}`,
      "Access-Control-Expose-Headers": "Content-Disposition",
    });
    ctx.body = Buffer.from(result.content);
  }

  async export() {
    const { ctx } = this;
    const workbook = new Excel.Workbook();
    workbook.creator = "test";
    workbook.lastModifiedBy = "test";
    workbook.created = new Date();
    workbook.modified = new Date();
    const sheet = workbook.addWorksheet("2018-10报表");
    sheet.columns = [
      { header: "创建日期", key: "create_time", width: 15 },
      { header: "单号", key: "id", width: 15 },
      { header: "电话号码", key: "phone", width: 15 },
      { header: "地址", key: "address", width: 15 },
    ];
    const data = [
      {
        create_time: "2018-10-01",
        id: "787818992109210",
        phone: "11111111111",
        address: "深圳市",
      },
    ];
    sheet.addRows(data);
    const buffer = await workbook.xlsx.writeBuffer("test.xlsx");
    ctx.attachment("test.xlsx");
    ctx.set({
      "Access-Control-Expose-Headers": "Content-Disposition",
    });
    ctx.body = buffer;
  }

  // async fetch() {
  //   const { ctx } = this;
  //   const time = new Date().getTime();
  //   const url = "http: baidu.com"; // `https://opentest.kaikeba.com/enterprise-wechat/wx/wechat-config/1?js_url=https://testewx.kaikeba.com/queryform?id=1&time=${time}`;
  //   console.log("url", url);
  //   const result = await ctx.curl(url, {
  //     // 自动解析 JSON response
  //     dataType: "json",
  //     // 3 秒超时
  //     timeout: 10000,
  //   });
  //   result.time = time;
  //   ctx.body = result;
  // }
}

module.exports = UserController;
